package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import model.*;



public class StudentForm extends ActionForm {
	
	private enum Mappings{
		NAME,
		ADDRESS,
		RESIDENCY_RADIO,
		FINALIZE_DEGREE,
		DEFAULT
		
		
	}

	private static final long serialVersionUID = 1L;
	
	private String firstName , middleInitial , lastName;
	private String street, city, state, countryCode, zip, areaCode, phone;
	private String permanentResident;
	private String degreeGPA;

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setMiddleInitial(String middleInitial) {
		this.middleInitial = middleInitial;
	}

	
	public String getMiddleInitial() {
		return middleInitial;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}
	
	

	
	
	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPermanentResident() {
		return permanentResident;
	}

	public void setPermanentResident(String permanentResident) {
		this.permanentResident = permanentResident;
	}

	public String getDegreeGPA() {
		return degreeGPA;
	}

	public void setDegreeGPA(String degreeGPA) {
		this.degreeGPA = degreeGPA;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setFirstName(null);
		setMiddleInitial(null);
		setLastName(null);
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		
		ActionErrors errors = new ActionErrors();
		Mappings currentMapping;
		
		if(mapping.getPath().equals("/pages/students/countryOfCitizenship"))
		{
			currentMapping = Mappings.NAME;
		}
		else if (mapping.getPath().equals("/pages/students/USResident"))
		{
			currentMapping = Mappings.ADDRESS;
		}
		else if (mapping.getPath().equals("/pages/students/ResidencyRadio"))
		{
			currentMapping = Mappings.RESIDENCY_RADIO;
		}
		else if (mapping.getPath().equals("/pages/students/FinalizeDegree"))
		{
			currentMapping = Mappings.FINALIZE_DEGREE;
		}
		else
		{
			currentMapping = Mappings.DEFAULT;
		}
		
		
		
		switch(currentMapping)
		{
			case NAME:
			{
				if ((firstName == null) || (firstName.length() < 1)){
					errors.add("firstMsgTag1", new ActionMessage("errors.required","First Name"));
					
				}

				if ((lastName == null) || (lastName.length() < 1)){
					errors.add("lastMsgTag1", new ActionMessage("errors.required","Last Name"));
					
				}
				
				boolean containsOnlyLetters;
				
				containsOnlyLetters = containsOnlyAlpha(firstName);
				if ( ! containsOnlyLetters ){
					errors.add("firstMsgTag2", new ActionMessage("errors.alpha", "First Name"));
					
				}
				
				containsOnlyLetters = containsOnlyAlpha(middleInitial);
				if ( ! containsOnlyLetters ){
					errors.add("middleMsgTag2", new ActionMessage("errors.alpha","Middle Initial"));
					
				}
				
				containsOnlyLetters = containsOnlyAlpha(lastName);
				if ( ! containsOnlyLetters ){
					errors.add("lastMsgTag2", new ActionMessage("errors.alpha","Last Name"));
				}

				if(errors.isEmpty())
				{
					
				}
				
				break;
			}
			case ADDRESS:
			{
				if ((street == null) || (street.length() < 1)){
					errors.add("streetMsgTag1", new ActionMessage("errors.required","Street Address"));
					
				}

				if ((city == null) || (city.length() < 1)){
					errors.add("cityMsgTag1", new ActionMessage("errors.required","City"));
					
				}
				if ( (state == null) || (countryCode == null) ){
					
					if( (state != null) && state.length() < 1)
					{
						errors.add("stateMsgTag1", new ActionMessage("errors.required","State"));
					}
					
					else if( (countryCode != null) && countryCode.length() < 1)
					{
						errors.add("countryCodeMsgTag1", new ActionMessage("errors.required","Country Code"));
					}
						
					
					
				}

				if ((zip == null) || (zip.length() < 1)){
					errors.add("zipMsgTag1", new ActionMessage("errors.required","ZIP code"));
					
				}
				
				if ((areaCode == null) || (areaCode.length() < 1)){
					errors.add("areaCodeMsgTag1", new ActionMessage("errors.required","Area Code"));
					
				}

				if ((phone == null) || (phone.length() < 1) ){
					errors.add("phoneMsgTag1", new ActionMessage("errors.required","Phone Number"));
					
				}
				
				
				if(!containsOnlyNumeric(zip))
				{
					errors.add("zipMsgTag2", new ActionMessage("errors.numeric", "Zip Code"));
				}
				
				if(!containsOnlyNumeric(areaCode))
				{
					errors.add("areaCodeMsgTag2", new ActionMessage("errors.numeric", "Area Code"));
				}
				
				if(!validPhone(phone))
				{
					errors.add("phoneMsgTag2", new ActionMessage("errors.numeric.Phone"));
				}
				
				
				
				break;
			}
			case RESIDENCY_RADIO:
			{
				if( permanentResident == null)
				{
					errors.add("permaResTag", new ActionMessage("errors.radio"));
				}
				break;
			}
			case FINALIZE_DEGREE:
			{
				if((degreeGPA == null) || (degreeGPA.length() < 1))
				{
					errors.add("gpaTag1", new ActionMessage("errors.required", "GPA"));
				}
				
				if(!validGPA(degreeGPA))
				{
					errors.add("gpaTag2", new ActionMessage("errors.gpa"));
				}
				
				request.setAttribute("crsDisc", StudentDBModel.selectAllDisciplines());
				break;
			}
			default:
			{
				System.err.println("NO CASE FOR MAPPING " + currentMapping);
				break;
			}
		
		}
		
		return errors;
		
	}
	
	public static boolean
	containsOnlyAlpha( String str ){
		if( str == null ) return false;
		for( int i=0; i<str.length(); i++ ){ 
			if( ! Character.isLetter( str.charAt(i) ) ){ 
				return false;
			}
		}
		return true;
	}
	
	public static boolean
	containsOnlyNumeric( String str ){
		if( str == null ) return false;
		for( int i=0; i<str.length(); i++ ){ 
			if( ! Character.isDigit( str.charAt(i) ) ){ 
				return false;
			}
		}
		return true;
	}
	
	public static boolean
	validPhone( String str ){
		if( str == null ) return false;
		for( int i=0; i<str.length(); i++ ){ 
			if( !Character.isDigit( str.charAt(i)) && (str.charAt(i) != ' ') && (str.charAt(i) != '-')) { 
				return false;
			}
		}
		return true;
	}
	
	public static boolean
	validGPA( String str ){
		if( str == null ) return false;
		for( int i=0; i<str.length(); i++ ){ 
			if( !Character.isDigit(str.charAt(i)) && (str.charAt(i) != '.')){ 
				return false;
			}
		}
		return true;
	}
	
}
